package com.sadam.ui4.Data;

import android.annotation.SuppressLint;
import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import java.text.SimpleDateFormat;
import android.media.MediaMetadataRetriever;
import android.util.Log;
import java.io.IOException;
import java.text.ParseException;
import java.util.Date;
import java.util.HashMap;
import java.util.Locale;

public class Video {
    private long id;
    private int likeCount;
    private int commentCount;
    private String title;
    private String introduction;
    private String label;
    private String avatarUrl;      // 用户头像 URL（来自 User 表）
    private String userName;       // 用户名（来自 User 表）
    private long userId;           // 外键
    private User user;             // 关联的 User 对象
    // 新增：视频路径（对应Video表的videoFilePath字段，核心播放地址）
    private String videoFilePath;
    private Bitmap cover; // 新增：存储视频封面图
    private String url;
    private String userAvatarUrl;
    private String publishTime; // 发布时间戳

    // ✅ 核心构造函数：新增videoFilePath参数，匹配视频表结构
    public Video(long id, int likeCount, int commentCount,
                 String title, String introduction, String label,
                 String avatarUrl, String userName, long userId,
                 User user, String videoFilePath) {
        this.id = id;
        this.likeCount = likeCount;
        this.commentCount = commentCount;
        this.title = title;
        this.introduction = introduction;
        this.label = label;
        this.avatarUrl = avatarUrl;
        this.userName = userName;
        this.userId = userId;
        this.user = user;
        this.videoFilePath = videoFilePath; // 初始化视频路径

    }

    // 【新增】用于模拟数据的简化构造函数（无User关联时使用）
    public Video(long id, int likeCount, int commentCount,
                 String title, String introduction, String label,
                 String avatarUrl, String userName, long userId,
                 String videoFilePath) {
        this(id, likeCount, commentCount, title, introduction, label,
                avatarUrl, userName, userId, null, videoFilePath);
    }

    // -------------------------- 新增getter/setter（视频路径） --------------------------
    public String getVideoFilePath() {
        return videoFilePath;
    }

    public void setVideoFilePath(String videoFilePath) {
        this.videoFilePath = videoFilePath;
    }

    // -------------------------- 原有getter/setter（保持不变） --------------------------
    public long getId() { return id; }
    public void setId(long id) { this.id = id; }

    public int getLikeCount() { return likeCount; }
    public void setLikeCount(int likeCount) { this.likeCount = likeCount; }

    public int getCommentCount() { return commentCount; }
    public void setCommentCount(int commentCount) { this.commentCount = commentCount; }

    public String getTitle() { return title; }
    public void setTitle(String title) { this.title = title; }

    public String getIntroduction() { return introduction; }
    public void setIntroduction(String introduction) { this.introduction = introduction; }

    public String getLabel() { return label; }
    public void setLabel(String label) { this.label = label; }

    public String getAvatarUrl() { return avatarUrl; }
    public void setAvatarUrl(String avatarUrl) { this.avatarUrl = avatarUrl; }

    public String getUserName() { return userName; }
    public void setUserName(String userName) { this.userName = userName; }

    public long getUserId() { return userId; }
    public void setUserId(long userId) { this.userId = userId; }

    public User getUser() { return user; }
    public void setUser(User user) { this.user = user; }
    // 【新增】静态方法：生成视频封面（第一帧）
    public static Bitmap getVideoThumb(String videoFilePath) throws IOException {
        Bitmap bitmap = null;
        MediaMetadataRetriever retriever = new MediaMetadataRetriever();
        try {
            // 支持本地和网络视频
            retriever.setDataSource(videoFilePath, new HashMap<>());
            bitmap = retriever.getFrameAtTime(); // 获取第一帧
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Video", "网络视频封面生成失败，尝试本地逻辑");
            try {
                retriever.setDataSource(videoFilePath);
                bitmap = retriever.getFrameAtTime();
            } catch (Exception e1) {
                e1.printStackTrace();
                throw new IOException("生成视频封面失败", e1);
            }
        } finally {
            retriever.release(); // 释放资源，避免内存泄漏
        }
        return bitmap;
    }

    // 【新增】获取封面图（若未生成则自动生成）
    public Bitmap getCover() throws IOException {
        if (cover == null) {
            cover = getVideoThumb(this.videoFilePath); // 用视频路径生成封面
        }
        return cover;
    }

    // 【新增】重新生成封面图（对应getVideoThumb()调用）
    public Bitmap getVideoThumb() throws IOException {
        this.cover = getVideoThumb(this.videoFilePath);
        return this.cover;
    }
    public String getUrl() {
        return url;
    }

    public String getUserAvatarUrl() {
        return userAvatarUrl;
    }

    // Setter 方法（如果需要）
    public void setUrl(String url) {
        this.url = url;
    }

    public void setUserAvatarUrl(String userAvatarUrl) {
        this.userAvatarUrl = userAvatarUrl;
    }
    public long getPublishTime() {
        return parseTimeStringToTimestamp(publishTime);
    }
    /**
     * 将时间字符串转换为时间戳
     * 支持格式："2025-10-18 21:03:43"
     */
    private static long parseTimeStringToTimestamp(String timeString) {
        if (timeString == null || timeString.trim().isEmpty()) {
            return System.currentTimeMillis(); // 返回当前时间作为默认值
        }

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault());
            Date date = sdf.parse(timeString.trim());
            return date != null ? date.getTime() : System.currentTimeMillis();
        } catch (ParseException e) {
            Log.e("TimeParser", "时间格式解析失败: " + timeString, e);
            return System.currentTimeMillis();
        }
    }
    // 在 Video 类中添加这些方法
    public String getDescription() {
        return introduction; // 根据数据库字段，description 对应 introduction
    }

    public String getTags() {
        return label; // 根据数据库字段，tags 对应 label
    }



}